/**2013-4-10
 zhangqj
 * 
 */
package com.data;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

/**
 * @author zhangqj 2013-4-10
 */
public class RSet {
	private ResultSet rs = null;
	private SMent stmt = null;
	private DBConnection conns = null;

	public RSet() {
		conns = new DBConnection();
		stmt = new SMent();
	}

	public ResultSet getResultSet(DatabaseMetaData DBMD) {
		// rs=DBMD.getBestRowIdentifier(catalog, schema, table, scope, nullable)
		return rs;
	}

	public ResultSet getExecuteQuery(Statement stmt, String sql) {
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}

	/*
	 * show database by argument
	 */
	public void showData(ResultSet rs, String arg) {
		try {
			while (rs.next()) {		 
					try {
						System.out.println(rs.getString(arg)+" ");
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}			 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void showData(ResultSet rs, int count) {
		try {
			System.out.println("-----------------------------------------------");
			while (rs.next()) {
				for (int i = 1; i <= count; i++)
					try {
						System.out.print(rs.getString(i)+" | ");

					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				System.out.println("   ");
				System.out.println("-----------------------------------------------");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param sql
	 */
	public void showAllData(String sql) {
		ResultSet rss=this.getExecuteQuery(stmt.getStatement(this.conns.getConnection()), sql);
		int count = 0;
		DataMeta DM = new DataMeta();
		count = DM.CountColumn(rss); 
		System.out.println(count);
		this.showData(rss, count);
		try {
			rss.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void showRS(String sql, String arg) {
		this.showData(this.getExecuteQuery(
				stmt.getStatement(conns.getConnection()), sql), arg);

	}

	public void closeRS() {
		try {
			if (rs != null)
				this.rs.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		stmt.closeStmt();
		conns.closeConnection(conns.getConnection());
	}

	public ResultSet getRs() {
		return rs;
	}

	public void setRs(ResultSet rss) {
		this.rs = rs;
	}
}
